/*
2012-01-12 by jieh
 1. 取消 # 字元分隔處理。
*/

package idv.jieh.commons.util;

import java.util.regex.*;

public class PhoneNumberRepair {

  public static enum PHONE_KIND{CELL, TELEPHONE, NOTHING}
  public static enum COUNTRY{TAIWAN}

  public String fix(String phone){

    Pattern patt = Pattern.compile("(\\d+)");
    Matcher match = patt.matcher(phone);
    // 完全沒數字碼的踢掉!!!
    if(match.groupCount() == 0) return null;

    // 如果不是手機或免費電話，當號碼超過七碼，則將剩餘的數字視為分機接回，並插入 # 字元分隔。
    StringBuilder new_phone = new StringBuilder();
    while(match.find()){
      // 因為判斷 7 碼就加入 # 是錯誤的判斷，還要考慮區碼和各縣市號碼有7or8碼的狀況，暫時取消 # 字元分隔處理。
//      if(new_phone.length() >= 7 && !(phone.startsWith("09") || phone.startsWith("080")))
//        new_phone.append("#");
      new_phone.append(match.group());
    }

    // 號碼太短直接出局
    if (new_phone.length() <= 2) return null;
    // 不可能的電話 (這是沒考慮國際電話)
    if (new_phone.indexOf("00") == 0) return null;

    // 去掉本地區碼
    if (new_phone.indexOf(localCode) == 0 && cut_local_code) new_phone.delete(0, 2);
    // 外縣市電話長度不夠
    else if(new_phone.indexOf("0") == 0 && new_phone.length() < 6) return null;

    // 手機開頭碼長度不夠
    if ((new_phone.indexOf("09") == 0 || new_phone.indexOf("080") == 0) && new_phone.length() < 10) return null;
    // 夠的話就切出手機號碼
    else if (new_phone.indexOf("09") == 0) new_phone.delete(10, new_phone.length());
    // 夠的話就把主電話用 # 分隔開
//    else if (new_phone.indexOf("0800") == 0) new_phone.insert(10, "#");

    return new_phone.toString();
  }

  private String localCode = "07";
  private boolean cut_local_code = true;
  private COUNTRY country = COUNTRY.TAIWAN;
  public void setLocalCode(String localCode) {this.localCode = localCode;}
  public void setCutLocalCode(boolean b){ cut_local_code = b; }
  public String getLocalCode(){return localCode;}
  public void setCountry(COUNTRY country){this.country = country;}

  public static void main(String... args){
    PhoneNumberRepair me = new PhoneNumberRepair();
    String phone = "07123456";
    System.out.println(phone + " : 踹市內電話過濾 : " + me.fix(phone));

    phone = "    99    ";
    System.out.println(phone + " : 踹空白太多 : " + me.fix(phone));

    phone = "07-537-8000*123#223";
    System.out.println(phone + " : 踹智慧整頓號碼 : " + me.fix(phone));

    // 未考慮到國際電話的下場！
    phone = "886915-333-555223";
    System.out.println(phone + " : 踹智慧整頓號碼 : " + me.fix(phone));

    phone = "0915-333-555*223";
    System.out.println(phone + " : 踹智慧整頓號碼 : " + me.fix(phone));
    phone = "0800-058-787*377";
    System.out.println(phone + " : 踹智慧整頓號碼 : " + me.fix(phone));
    phone = "080-058-787*";
    System.out.println(phone + " : 踹智慧整頓號碼 : " + me.fix(phone));
    phone = "080-058-787*22";
    System.out.println(phone + " : 踹智慧整頓號碼 : " + me.fix(phone));

    phone = "08-7881235";
    System.out.println(phone + " : 踹智慧整頓號碼 : " + me.fix(phone));
    phone = "060-123456";
    System.out.println(phone + " : 踹智慧整頓號碼 : " + me.fix(phone));
    phone = "我是故意來亂的!!!00";
    System.out.println(phone + " : 踹智慧整頓號碼 : " + me.fix(phone));
    phone = "(02)87922-2088";
    System.out.println(phone + " : 踹智慧整頓號碼 : " + me.fix(phone));
    phone = "(07)87922-2088";
    System.out.println(phone + " : 踹智慧整頓號碼 : " + me.fix(phone));
  }

}
